package com.xin.di.uav.applet.utils;

import com.xin.di.uav.applet.controller.TtTesTerminalRangeController;
import com.xin.di.uav.applet.controller.TtTesTerminalRangeCoordinateController;
import com.xin.di.uav.applet.po.TtTesTerminal;
import com.xin.di.uav.applet.po.TtTesTerminalRange;
import com.xin.di.uav.applet.po.TtTesTerminalRangeCoordinate;
import com.xin.di.uav.applet.rest.TtTesTerminalRest;
import com.xin.di.uav.common.utils.AreaOverlapCheckUtils;
import com.xin.di.uav.common.utils.DistanceHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 航站信息，用于维护要展示的航站列表数据 前端控制器
 * </p>
 *
 * @author LTQ
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/home/airport/")
public class AirportInfoUtils {


    @Autowired
    private TtTesTerminalRest ttTesTerminalRest;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private TtTesTerminalRangeController ttTesTerminalRangeController;

    @Autowired
    private TtTesTerminalRangeCoordinateController ttTesTerminalRangeCoordinateController;



    public List<Long> obtainTheNearestTerminalWithinTheRangeNow(@RequestParam double longitude, @RequestParam double latitude) {
        List<Long> targetIds = new ArrayList<>();
        AreaOverlapCheckUtils.Location targetLocation = new AreaOverlapCheckUtils.Location(longitude, latitude);
        List<TtTesTerminal> ttTesTerminals = ttTesTerminalRest.getTtTesTerminals();
        if (!CollectionUtils.isEmpty(ttTesTerminals)) {
            List<Long> ttTesTerminalsId = ttTesTerminals.stream().map(ttTesTerminal -> ttTesTerminal.getId()).collect(Collectors.toList());
            List<TtTesTerminalRange> ttTesTerminalRanges = ttTesTerminalRangeController.getByTerminalId(ttTesTerminalsId);
            if (!CollectionUtils.isEmpty(ttTesTerminalRanges)) {
                List<Long> ttTesTerminalRangesId = ttTesTerminalRanges.stream().map(ttTesTerminalRange -> ttTesTerminalRange.getId()).collect(Collectors.toList());
                List<TtTesTerminalRangeCoordinate> ttTesTerminalRangeCoordinates = ttTesTerminalRangeCoordinateController.getByTerminalIdAndRangeId(ttTesTerminalsId, ttTesTerminalRangesId);
                for (TtTesTerminal ttTesTerminal : ttTesTerminals) {
                    for (TtTesTerminalRange ttTesTerminalRange : ttTesTerminalRanges) {
                        List<TtTesTerminalRangeCoordinate> sonTtTesTerminalRangeCoordinates = ttTesTerminalRangeCoordinates.stream().filter(ttTesTerminalRangeCoordinate -> ttTesTerminalRangeCoordinate.getRangeId().equals(ttTesTerminalRange.getId()) && ttTesTerminalRangeCoordinate.getTerminalId().equals(ttTesTerminal.getId())).collect(Collectors.toList());
                        List<AreaOverlapCheckUtils.Location> locationList2 = new ArrayList<>();
                        sonTtTesTerminalRangeCoordinates = sonTtTesTerminalRangeCoordinates.stream().sorted(Comparator.comparingInt(TtTesTerminalRangeCoordinate::getOrderNo)).collect(Collectors.toList());
                        for (TtTesTerminalRangeCoordinate ttTesTerminalRangeCoordinate : sonTtTesTerminalRangeCoordinates) {
                            AreaOverlapCheckUtils.Location location = new AreaOverlapCheckUtils.Location(Double.valueOf(ttTesTerminalRangeCoordinate.getLongitude()), Double.valueOf(ttTesTerminalRangeCoordinate.getLatitude()));
                            locationList2.add(location);
                        }
                        if (!CollectionUtils.isEmpty(locationList2)) {
                            Byte shape = ttTesTerminalRange.getShape();
                            if (shape == 1) {
                                //多边形
                                if (AreaOverlapCheckUtils.isPointInPolygon(targetLocation, locationList2)) {
                                    targetIds.add(ttTesTerminal.getId());
                                    break;
                                }
                            } else if (shape == 2) {
                                //圆形
                                if (!CollectionUtils.isEmpty(sonTtTesTerminalRangeCoordinates)) {
                                    TtTesTerminalRangeCoordinate ttTesTerminalRangeCoordinate = sonTtTesTerminalRangeCoordinates.get(0);
                                    Object radius = ttTesTerminalRangeCoordinate.getRadius();
                                    String centerOfCircleLatitude = ttTesTerminalRangeCoordinate.getLatitude();
                                    String centerOfCircleLongitude = ttTesTerminalRangeCoordinate.getLongitude();
                                    //圆形
                                    double distance = DistanceHelper.getDistance(Double.valueOf(centerOfCircleLatitude), Double.valueOf(centerOfCircleLongitude), latitude, longitude);
                                    if (distance * 1000 < (Double) radius) {
                                        targetIds.add(ttTesTerminal.getId());
                                        break;
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }
        return targetIds;
    }
}